﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.LMS.Domain;
using NHibernate.Criterion;
using NHibernate;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

namespace ZDSoft.LMS.Manager
{
    public class TransportOrderManager : ManagerBase<TransportOrder>
    {
        public IList<string> FindDistinctYear()
        {
            IList<string> years = new List<string>();
            IList<TransportOrder> orders = Find(new List<ICriterion>() { Expression.Eq("Status", (int)TransportOrderStatus.Done) });
            foreach (TransportOrder to in orders)
            {
                if (years.Where(o => o.ToString() == to.CreateDate.ToShortDateString()).Count() > 0)
                {
                    continue;
                }
                years.Add(to.CreateDate.Year.ToString());
            }
            return years;
        }

        #region hql测试 create by lengyahong

        public TransportOrder GetTransportOrderTest(string startStation, string provinceName, int creatorID)
        {
            TransportOrder order = null;

            //定义hql语句
            StringBuilder hql = new StringBuilder(); 

            #region hql定义
            //最简单的写法
            hql.Append("from TransportOrder tOrder");
            hql.Append(" where tOrder.StartStation = :StartStation ");
            hql.Append(" and tOrder.ProvinceOfDestionation.Name = :ProvinceName");//注意此句，设置关联属性的属性为条件，只能使用[Blongsto]属性
            hql.Append(" and tOrder.Creator.ID = :CreatorID");//注意此句，设置关联属性的属性为条件，只能使用[Blongsto]属性

            /*
            //关联属性写法，初学者不推荐这样写
            hql.Append("from TransportOrder tOrder");
            hql.Append(" left outer join tOrder.ProvinceOfDestionation as pd");
            hql.Append(" left outer join tOrder.Creator as c");
            hql.Append(" where tOrder.StartStation = :StartStation");
            hql.Append(" and pd.Name = :ProvinceName");
            hql.Append(" and c.ID = :CreatorID");//注意此句，设置关联属性的属性为条件，只能使用[Blongsto]属性
             */
            #endregion

            #region 查询
            //第一种方法，很灵活
            //获取管理User的session对象
            using (ISession session = ActiveRecordBase.holder.CreateSession(typeof(TransportOrder)))
            {
                //创建执行对象
                IQuery query = session.CreateQuery(hql.ToString());

                //给参数赋值
                query.SetParameter("StartStation", startStation);
                query.SetParameter("ProvinceName", provinceName);
                query.SetInt32("CreatorID", creatorID);

                //执行查询，并返回满足条件的第一条数据，如果对象不存在，则返回null
                order = query.List<TransportOrder>().FirstOrDefault();
            }

            /*
            //第2中方法，使用简单，相对死板
            //使用SimpleQuery查询，该查询的结果为数组，即多行数据
            SimpleQuery query = new SimpleQuery(typeof(TransportOrder), hql.ToString());
            
            //给参数赋值
            query.SetParameter("StartStation", startStation);
            query.SetParameter("ProvinceName", provinceName);
            query.SetParameter("CreatorID", creatorID);
             
            //执行并返回查询结果
            TransportOrder[] list = (TransportOrder[])ActiveRecordBase.ExecuteQuery(query);//注意这句话的使用
            if (list != null && list.Length > 0)
                order = list[0];
            */
            #endregion

            return order;
        }

        public IList<TransportOrder> GetTransportOrderTest(string startStation, string provinceName, string receiveName)
        {
            //定义hql语句
            StringBuilder hql = new StringBuilder();
            StringBuilder where = new StringBuilder();
            hql.Append("from TransportOrder tOrder");

            //多条件组合查询的方法，设置查询条件
            if (!string.IsNullOrEmpty(startStation))
            {
                where.Append(" where tOrder.StartStation = :StartStation ");
            }
            if (!string.IsNullOrEmpty(provinceName))
            {
                if (where.Length > 0) where.Append(" and ");
                where.Append("tOrder.ProvinceOfDestionation.Name = :ProvinceName");//注意此句，设置关联属性的属性为条件，只能使用[Blongsto]属性
            }
            if (!string.IsNullOrEmpty(receiveName))
            {
                if (where.Length > 0) where.Append(" and ");
                where.Append("tOrder.Receiver.Name = :ReceiveName");//注意此句，设置关联属性的属性为条件，只能使用[Blongsto]属性
            }
            //增加查询条件
            hql.Append(where);


            SimpleQuery query = new SimpleQuery(typeof(TransportOrder), hql.ToString());

            //给参数赋值
            if (!string.IsNullOrEmpty(startStation))
            {
                query.SetParameter("StartStation", startStation);
            }
            if (!string.IsNullOrEmpty(provinceName))
            {
                query.SetParameter("ProvinceName", provinceName);
            }
            if (!string.IsNullOrEmpty(receiveName))
            {
                query.SetParameter("ReceiveName", receiveName);
            }

            //执行并返回查询结果
            IList<TransportOrder> list = ActiveRecordBase.ExecuteQuery(query) as IList<TransportOrder>;
            return list;
        }
        #endregion
    }
}
